/**
 * Created by liekkas on 2018/6/27.
 */
import axios from 'axios';      // 引入axios
import qs from 'qs';            // 引入qs
import api from './api'; // 引入api
import {Spin,Message}from 'iview';
//基础配置
axios.defaults.timeout       = 30000;             // 超时默认值
axios.defaults.baseURL       = api.baseURL;   // 默认baseURL
axios.defaults.responseType  = 'json'         // 默认数据响应类型
// axios.defaults.withCredentials = true;           // 表示跨域请求时是否需要使用凭证
// http request 拦截器
// 在ajax发送之前拦截 比如对所有请求统一添加header token
axios.interceptors.request.use(
    config => {
        let headers={
            "Content-Type":"application/json;charset=UTF-8"
        };
        Object.assign(headers, api.headers);
        config.headers=headers;
        Spin.show({
            render: (h) => {
                return h('div', [
                    h('Icon', {
                        'class': 'demo-spin-icon-load',
                        props: {
                            type: 'load-c',
                            size: 18
                        }
                    }),
                    h('div', 'Loading')
                ])
            }
        });
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// http response 拦截器
// ajax请求回调之前拦截 对请求返回的信息做统一处理 比如error为401无权限则跳转到登陆界面
axios.interceptors.response.use(
    response => {
        Spin.hide();
        if(typeof response.data!="undefined"&&response.data!=null&&response.data.code){
            switch (response.data.code) {
                case 500:{
                    Message.warning({content:response.data.msg,duration:3});
                    break;
                }
                case 200:{
                    content:response.data.msg!=""&&response.data.msg!="SUCCESS"&&Message.success({content:response.data.msg,duration:3});
                    break;
                }
                default:
                    break;
            }
        }
        return response;
    },
    error => {
        Spin.hide();
        return Promise.reject(error);
    }
);

export default axios;

/**
 * post 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data).then(response => {
                typeof response!='undefined'&&resolve(response.data);
            }, err => {
                reject(err);
            });
    });
}

/**
 * post 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function postFormData (url, data = {}) {
    return new Promise((resolve, reject) => {
        data=qs.stringify(data);
        axios.post(url+"?"+data)
            .then(response => {
                typeof response!='undefined'&&resolve(response.data);
            }, err => {
                reject(err);
            });
    });
}
/**
 * get 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params:data,responseType:"text"}).then(response => {
                typeof response!='undefined'&&resolve(response.data);
            }, err => {
                reject(err);
            });
    });
}

/**
 * put 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                typeof response!='undefined'&&resolve(response.data);
            }, err => {
                reject(err);
            });
    });
}

/**
 * delete 方法封装
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch (url, data = {}) {
    return new Promise((resolve, reject) => {
        data=qs.stringify(data);
        axios.delete(url+"?"+data)
            .then(response => {
                typeof response!='undefined'&&resolve(response.data);
            }, err => {
                reject(err);
            });
    });
}

